//
//  AppGoodsListUnitController.m
//  gungunchou
//
//  Created by lyzbmacos3 on 2018/12/6.
//  Copyright © 2018年 xskj. All rights reserved.
//

#import "AppGoodsListUnitController.h"
#import "GoodsCell.h"
#import "HomeIndexModel.h"
#import "AppGoodsDetailController.h"

@interface AppGoodsListUnitController () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collect;
@property (nonatomic, assign) NSInteger pageIndex;
@property (nonatomic, copy) NSString *catId;
@property (nonatomic, strong) NSMutableArray <ProductModel *> *dataArr;
@end

@implementation AppGoodsListUnitController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.pageIndex = 1;
    self.catId = self.model.CateID;
    
    [self.collect registerNib:[UINib nibWithNibName:@"GoodsCell" bundle:nil] forCellWithReuseIdentifier:@"GoodsCell"];
    self.collect.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.pageIndex += 1;
        [self fetchList];
    }];
    
    [self fetchList];
}

- (NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = NSMutableArray.new;
    }
    return _dataArr;
}

- (void)endrefresh
{
    [self.collect.mj_header endRefreshing];
    [self.collect.mj_footer endRefreshing];
}

- (void)fetchList
{
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:@(self.pageIndex),@"PageIndex", @"10",@"PageSize", self.catId,@"Cid", nil];
    [MBProgressHUD showHUDAddedTo:KWindow animated:YES];
    [[CGNetManage shared] httpGetGoodsListWithParams:params Completed:^(NSArray *data) {
        [MBProgressHUD hideHUDForView:KWindow animated:YES];
        [self endrefresh];
        if (data && data.count > 0) {
            for (NSDictionary *proDic in data) {
                ProductModel *model = [ProductModel mj_objectWithKeyValues:proDic];
                [self.dataArr addObject:model];
            }
            
            [self.collect reloadData];
        }
        else {
            self.pageIndex -= 1;
        }
        
    } fail:^(NSError *err) {
        [MBProgressHUD hideHUDForView:KWindow animated:YES];
        [self endrefresh];
        self.pageIndex -= 1;
    }];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ProductModel *productModel = [self.dataArr objectAtIndex:indexPath.row];
    GoodsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GoodsCell" forIndexPath:indexPath];
    cell.productModel = productModel;
    return cell;
}


#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ProductModel *productModel = [self.dataArr objectAtIndex:indexPath.row];
    AppGoodsDetailController *con = AppGoodsDetailController.new;
    con.model = productModel;
    con.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:con animated:YES];
}


#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((SCREEN_WIDTH-10)/2.0,240);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsZero;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}
@end
